package cn.shyouchi.wuguanjia.android.ui.workshow;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.show.ShowApi;
import cn.shyouchi.wuguanjia.android.event.LikeChange;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2017/3/1 at 11:39
 * Email: 472279981@qq.com
 * Des:
 */

public class WorkShowAdapter extends BaseMultiItemQuickAdapter<WorkShowEntity, BaseViewHolder> implements BGASortableNinePhotoLayout.Delegate {

    Activity mActivity;


    public WorkShowAdapter(List<WorkShowEntity> data, Activity activity) {
        super(data);
        addItemType(1, R.layout.item_work_show_layout);
        addItemType(2, R.layout.item_work_show_video_layout);
        this.mActivity = activity;


    }

    @Override
    protected void convert(BaseViewHolder holder, final WorkShowEntity entity) {

        holder.setText(R.id.tv_project_name, entity.getProjectname());
        holder.setText(R.id.tv_show_rank, String.format("第%s名", entity.getRank()));
        holder.setText(R.id.tv_show_content, entity.getTheme());
        holder.setText(R.id.tv_show_time, entity.getTime());
        holder.setText(R.id.ra_like, String.valueOf(entity.getLike()));
        ImageView img = holder.getView(R.id.img_publisher_header);
        TextView raLike = holder.getView(R.id.ra_like);
        raLike.setSelected(entity.getPraise() != 0);
        RatingBar ratingBar = holder.getView(R.id.rat_rank);
        if (entity.getRank() == 1) {
            ratingBar.setVisibility(View.VISIBLE);
            ratingBar.setNumStars(3);
        } else if (entity.getRank() == 2) {
            ratingBar.setVisibility(View.VISIBLE);
            ratingBar.setNumStars(2);
        } else if (entity.getRank() == 3) {
            ratingBar.setVisibility(View.VISIBLE);
            ratingBar.setNumStars(1);
        } else {
            ratingBar.setVisibility(View.INVISIBLE);
            ratingBar.setNumStars(0);
        }

        loadHead(Global.BASE_URL + entity.getId_card_head_image(), img);

        holder.getView(R.id.ra_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                likeShow(entity);
            }
        });

        ArrayList<String> imgs = new ArrayList<>();
        final String[] video = {""};

        try {
            for (String s : entity.getExtra()) {
                JSONObject imgJson = new JSONObject(s);
                if (imgJson.has("image")) {
                    imgs.add(Global.BASE_URL + imgJson.getString("image"));
                }
                if (imgJson.has("video")) {
                    video[0] = Global.BASE_URL + imgJson.getString("video");
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        switch (entity.getType()) {
            case 1:
                BGASortableNinePhotoLayout photoLayout = holder.getView(R.id.photo_snpl);
                photoLayout.setDeleteDrawableResId(R.drawable.photo_del_bg);
                photoLayout.init(mActivity);
                photoLayout.setDelegate(this);
                photoLayout.setIsSortable(false);
                photoLayout.setData(imgs);
                break;

            case 2:

                holder.getView(R.id.play_status).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

//                        video = "http://7xoulr.media1.z0.glb.clouddn.com/%E9%97%AA%E7%94%B5%E4%BE%A0.The.Flash.S02E01.%E4%B8%AD%E8%8B%B1%E5%AD%97%E5%B9%95.HDTVrip.1024X576.mp4";


                        mContext.startActivity(new Intent(mContext, VideoPlayerActivity.class).putExtra("path", video[0]));


                    }
                });


                break;
        }

    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .bitmapTransform(new GlideCircleTransform(mContext))
                .into(imageView);
    }

    @Override
    public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {

    }

    @Override
    public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {

    }

    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayout layout, View view, int position, String model, ArrayList<String> models) {
        mContext.startActivity(BGAPhotoPickerPreviewActivity.newIntent(mContext, layout.getMaxItemCount(), models, models, position, false));

    }

    private void likeShow(WorkShowEntity entity) {
        ShowApi showApi = new ShowApi(mContext);
        showApi.likeShow(String.valueOf(entity.getId()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtil.showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(BaseRequestResult baseRequestResult) {
                        EventBus.getDefault().post(new LikeChange());
                    }
                });
    }


}
